package com.dap.cms.governance.controller;

import com.dap.cms.governance.constant.ExcutorConstant;
import com.dap.cms.governance.po.CmsCompanyPo;
import com.dap.cms.governance.service.CmsCompanyService;
import com.dap.cms.governance.util.BeanValidator;
import com.dap.cms.governance.util.ExcutorUtils;
import com.dap.dao.model.Order;
import com.dap.dao.model.QueryResult;
import com.fesine.commons.entity.Result;
import com.fesine.commons.util.ResultUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/v1")
public class CmsCompanyController {
    private static final Logger logger = LoggerFactory.getLogger(CmsCompanyController.class);
    @Autowired
    private CmsCompanyService cmsCompanyService;

    /**
     * 新增CmsCompanyPo
     *
     * @param cmsCompanyPo
     * @return result
     */
    @PostMapping("/company")
    public Result addCmsCompany(CmsCompanyPo cmsCompanyPo) {
        BeanValidator.check(cmsCompanyPo);
        return ExcutorUtils.getResult(ExcutorConstant.OP_TYPE_ADD, cmsCompanyService, cmsCompanyPo, logger);
    }

    /**
     * 更新CmsCompanyPo
     *
     * @param cmsCompanyPo
     * @return result
     */
    @PutMapping("/company")
    public Result updateCmsCompany(CmsCompanyPo cmsCompanyPo) {
        if (null == cmsCompanyPo.getId()) {
            return ResultUtils.error(400, "数据请求错误");
        }
        return ExcutorUtils.getResult(ExcutorConstant.OP_TYPE_UPDATE, cmsCompanyService, cmsCompanyPo, logger);
    }



    /**
     * 查看CmsCompanyPo详情
     *
     * @param id
     * @return result
     */
    @GetMapping("/company/{id}")
    public Result getCmsCompany(@PathVariable("id") Integer id) {
        CmsCompanyPo cmsCompanyPo = new CmsCompanyPo();
        cmsCompanyPo.setId(id);
        cmsCompanyPo = cmsCompanyService.get(cmsCompanyPo);
        return ResultUtils.success(cmsCompanyPo);
    }

    /**
     * 查询CmsCompanyPo列表
     *
     * @param cmsCompanyPo
     * @return result
     */
    @GetMapping("/company")
    public Result listAll(CmsCompanyPo cmsCompanyPo) {
        cmsCompanyPo.addOrder(Order.asc("id"));

        List<CmsCompanyPo> list = cmsCompanyService.listAll(cmsCompanyPo);
        return ResultUtils.success(list);
    }

    /**
     * 分页查询CmsCompanyPo列表
     *
     * @param cmsCompanyPo
     * @param pageIndex
     * @param pageSize
     * @return result
     */
    @GetMapping("/company/page")
    public Result listPage(CmsCompanyPo cmsCompanyPo, @RequestParam(value = "pageIndex",
            required = false, defaultValue = "1")
            Integer pageIndex, @RequestParam(value = "pageSize", required = false, defaultValue =
            "10") Integer pageSize) {
        cmsCompanyPo.addOrder(Order.asc("id"));
        QueryResult<CmsCompanyPo> result = cmsCompanyService.listPage(cmsCompanyPo, pageIndex, pageSize);
        long total = result.getTotalrecord();
        Map<String, Object> map = new HashMap(16);
        if (total > 0) {
            map.put("totalRecord", total);
            map.put("resultList", result.getResultlist());
        }
        return ResultUtils.success(map);
    }
}

